package com.young.monitor.service.impl;

import com.young.common.util.StringUtils;
import com.young.interfaces.log.model.ApiMonitorDTO;
import com.young.interfaces.log.model.LogDTO;
import com.young.monitor.model.ApiIndex;
import com.young.monitor.model.ApiTrace;
import com.young.monitor.service.IApiIndexService;
import com.young.monitor.service.IApiTraceService;
import com.young.monitor.service.ILogCollectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日志收集服务
 *
 * @author imrookie
 * @date 2019/5/31
 */
@Service("logCollectService")
public class LogCollectServiceImpl implements ILogCollectService {

    private static final Logger logger = LoggerFactory.getLogger(LogCollectServiceImpl.class);

    /**
     * 全链路追踪服务
     */
    @Resource(name="apiTraceService")
    IApiTraceService apiTraceService;
    /**
     * 监控指标服务
     */
    @Resource(name="apiIndexService")
    IApiIndexService apiIndexService;

    /**
     * 处理日志
     *
     * @param logDTO
     */
    @Override
    public void handleLogDTO(LogDTO logDTO) {
        ApiTrace apiTrace = ApiTrace.newInstanceFromLogDTO(logDTO);
        apiTraceService.insert(apiTrace);
    }

    /**
     * 批量处理日志
     * @param logDTOList
     */
    @Override
    public void handleLogDTO(List<LogDTO> logDTOList) {
        if (logDTOList == null || logDTOList.isEmpty()) {
            logger.info("[批量处理日志] 日志集合为空.");
            return;
        }
        List<ApiTrace> list = new ArrayList<ApiTrace>(logDTOList.size());
        for (LogDTO logDTO : logDTOList) {
            list.add(ApiTrace.newInstanceFromLogDTO(logDTO));
        }
        //批量插入日志
        apiTraceService.batchInsert(list);
    }

    /**
     * 处理接口监控信息
     * @param apiMonitorDTOList
     */
    @Override
    public void handleApiMonitorDTO(List<ApiMonitorDTO> apiMonitorDTOList) {
        if (apiMonitorDTOList == null || apiMonitorDTOList.isEmpty()) {
            logger.info("[处理接口监控信息] 接口监控集合为空.");
            return;
        }
        List<ApiIndex> list = new ArrayList<ApiIndex>(apiMonitorDTOList.size());
        for (ApiMonitorDTO monitor : apiMonitorDTOList) {
            ApiIndex apiIndex = new ApiIndex();
            apiIndex.setKeyCode(monitor.getKey());
            apiIndex.setTp50(monitor.getTp50());
            apiIndex.setTp90(monitor.getTp90());
            apiIndex.setTp99(monitor.getTp99());
            apiIndex.setTp999(monitor.getTp999());
            apiIndex.setMinMs(monitor.getMinMs());
            apiIndex.setMaxMs(monitor.getMaxMs());
            apiIndex.setAvgMs(monitor.getAvgMs());
            apiIndex.setAvailability(monitor.getAvailability());
            apiIndex.setCallNum(monitor.getCallNum());
            apiIndex.setServerIp(monitor.getServerIp());
            apiIndex.setAppName(monitor.getAppName());
            apiIndex.setTimeMinute(monitor.getMinute());
            apiIndex.setTimeMinute5(monitor.getMinute() - (monitor.getMinute() % 5));
            apiIndex.setTimeHour(monitor.getMinute() / 100);
            apiIndex.setTimeDay(monitor.getMinute() / 10000);
            apiIndex.setTimeMonth(monitor.getMinute() / 1000000);
            apiIndex.setTimeYear(monitor.getMinute() / 100000000);
            list.add(apiIndex);
        }
        apiIndexService.batchInsert(list);
    }
}
